#!/bin/sh

# 用到的程序
git="git"
npm="npm"
cnpm="cnpm"

# 项目名称
projectName=""
# 项目目录 绝对路径
projectPath=""
# 部署目录 绝对路径
buildPath=""

# 保留备份数量
backupNum=5
# 备份目录
backupPath=${projectPath}/${projectName}/buildHistory

# 仓库地址
gitUrl=""
# 仓库分支
gitBranch="develop"

# 自检查
selfCheck () {

    # 环境检测
    if ! type ${npm} >/dev/null 2>&1; then
        echo "未安装 npm, 环境检测不通过"
        exit
    fi
    if ! type ${git} >/dev/null 2>&1; then
        echo "未安装 git, 环境检测不通过"
        exit
    fi

    # 项目目录上级
    if [ "$projectPath" = "" ]; then
        echo "projectPath 不能为空"
        exit
    elif [[ ! $projectPath ==  \/* ]]; then 
        echo "projectPath 必须是绝对路径"
        exit
    fi

    # 项目名称
    if [ "$projectName" == "" ]; then
        echo "projectName 不能为空"
        exit
    elif [ ! $(echo $projectName | grep "\/") == "" ]; then
        echo "projectName 不能包含 /"
        exit
    fi

    # 备份目录
    if [ "$backupPath" == "" ]; then
        echo "backupPath 不能为空"
        exit
    elif [[ ! $backupPath ==  \/* ]]; then 
        echo "backupPath 必须是绝对路径"
        exit
    fi

    # 部署目录
    if [ "$buildPath" == "" ]; then
        echo "buildPath 不能为空"
        exit
    elif [[ ! $buildPath ==  \/* ]]; then 
        echo "buildPath 必须是绝对路径"
        exit
    elif [ ! $(echo $buildPath | grep $projectPath/$projectName) == "" ]; then
        echo "buildPath 不能在 projectName 目录下"
        exit
    fi
}

createProject () {
    echo "正在创建项目"

    # 创建目录
    mkdir -p ${projectPath}/
    cd ${projectPath}/

    # 克隆项目
    ${git} clone -b ${gitBranch} ${gitUrl} ${projectName}
    cd ${projectName}

    cp .env .env.local
    echo "项目创建成功, 请前往 ${projectPath}/${projectName} 修改配置文件 .env.local, 随后执行 ${0} build 构建"
}

updateProject () {
    echo "更新项目"

    # git 更新
    ${git} pull
    if [ "$?" != "0" ]; then
        echo 'git pull failed.'
        exit
    fi
}

buildProject () {
    if ! type ${npm} >/dev/null 2>&1; then
        echo "未安装npm, 项目构建失败"
    else

        if [ ! -f .env.local ]; then
            cp .env .env.local
        fi

        echo "npm run build"
        ${npm} run build
        echo "项目构建成功, 构建路径 ${projectPath}/${projectName}/dist"
    fi
}

npmInstall () {

    if ! type ${npm} >/dev/null 2>&1; then
        echo "未安装npm, npm install 失败"
    else

        ${npm} --registry=https://registry.npm.taobao.org install

        # if type ${cnpm} >/dev/null 2>&1; then
        #     # cnpm
        #     echo "cnpm install"
        #     ${cnpm} install
        # else
        #     # npm
        #     echo "npm install"
            # ${npm} install
        # fi

        echo "npm install 成功"
    fi
}

moveToBuildPath () {

    # 项目备份目录
    mkdir -p ${backupPath}

    # 检查构建路径是否存在
    if [ -d ${buildPath} ]; then
        # 备份
        echo "正在备份"
        mv ${buildPath} ${backupPath}/backup-$(date "+%Y%m%d%H%M%S")
        echo "备份完成 "${backupPath}/backup-$(date "+%Y%m%d%H%M%S")
    fi

    # 防止父级目录不存在
    mkdir -p ${buildPath}
    rm -rf ${buildPath}

    # 部署
    echo "正在部署"
    mv ${projectPath}/${projectName}/dist ${buildPath}
    echo "项目部署完成, 部署路径 ${buildPath}"

    # 整理备份
    arrangeBackup
}

arrangeBackup () {
    echo "正在整理备份文件"
    cd ${backupPath}

    # 遍历目录打包成tar.gz
    for v in $(ls -tr)
    do
        if [ -d $v ]; then
            tar -czvf $v.tar.gz $v >/dev/null 2>&1
            rm -rf $v
        fi
    done
    # 保留最新的n个
    i=0
    for v in $(ls -t)
    do
        let i++
        if [ $i -gt $backupNum ]; then
            if [ -f $v ]; then
                rm -f $v
            fi
        fi
    done

    ls -lt
    echo "整理完毕 $backupPath"
}

printInfo () {
    echo ""
    echo ""
    echo "项目路径:  ${projectPath}/${projectName}"
    echo "备份路径:  ${backupPath}"
    echo "备份保留:  最近 ${backupNum} 次备份"
    echo "部署目录:  ${buildPath}"
}

# 自检测
selfCheck

if [ ! -d ${projectPath}/${projectName} ]; then
    createProject
else
    cd ${projectPath}/${projectName}
    case $1 in
        "create")
            echo "项目已创建, 如需重新创建, 请手动删除项目目录 ${projectPath}/${projectName} 后重新执行 ${0}"
        ;;
        "pull")
            updateProject
        ;;
        "update")
            updateProject
            npmInstall
            buildProject
            moveToBuildPath
        ;;
        "build")
            npmInstall
            buildProject
            moveToBuildPath
        ;;
        *)
            echo ""
            echo "usage: ${0} (create|update|...)"
            echo ""
            echo "    create  创建项目"
            echo "    pull    从仓库拉取"
            echo "    update  从仓库更新并构建"
            echo "    build   构建项目"

        ;;
    esac
fi

printInfo
